// 设置viewpost
(function(window){
	var devicePixelRatio = 1 || 1;
//	var devicePixelRatio = window.devicePixelRatio || 1;
	var scale = 1 / devicePixelRatio;
	var meta = window.document.createElement("meta");
	meta.name = "viewport";
	meta.content = "initial-scale=" + scale + ", maximum-scale=" + scale + ", minimum-scale=" + scale + ", user-scalable=no";
	window.document.head.appendChild(meta);
})(window);

// 设置rem
(function(doc, win){
	var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function(){
		var clientWidth = docEl.clientWidth;
		docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
	};
	if(!doc.addEventListener){
		return;
	}
	win.addEventListener(resizeEvt, recalc, false);
	doc.addEventListener('DOMContentLoaded', recalc, false);
	recalc();
})(document, window);

(function(window){
	window.Nuui = window.Nuui || {}; //全局对象 Nuui
	
	/**
	 * @memberof Nuui
	 * @name os
	 * @type {string}
	 * @desc 当前运行环境<br/>
	 * 		web - 网页版<br/>
	 * 		android - 安卓app<br/>
	 * 		ios - ios app
	 */
	Nuui.os = "web";
	
	/**
	 * @memberof Nuui
	 * @name ios
	 * @type {boolean}
	 * @desc 是否运行在苹果设备上
	 */
	Nuui.ios = false;
	
	/**
	 * @memberof Nuui
	 * @name android
	 * @type {boolean}
	 * @desc 是否运行在安卓上
	 */
	Nuui.android = false;
	
	/**
	 * @memberof Nuui
	 * @name iphone
	 * @type {boolean}
	 * @desc 是否运行在iphone上
	 */
	Nuui.iphone = false;
	
	/**
	 * @memberof Nuui
	 * @name ipad
	 * @type {boolean}
	 * @desc 是否运行在ipad上
	 */
	Nuui.ipad = false;
	
	/**
	 * @memberof Nuui
	 * @name phone
	 * @type {boolean}
	 * @desc 是否运行在app上
	 */
	Nuui.phone = false;
	
	/**
	 * @memberof Nuui
	 * @name osVersion
	 * @type {string}
	 * @desc ios版本号
	 */
	Nuui.osVersion = "";
	
	
	//获取设备信息
	var ua = navigator.userAgent;
	var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
	var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
	var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
	var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);
	var isNative = ua.indexOf("YNET")>=0;
	Nuui.ios = Nuui.android = Nuui.iphone = Nuui.ipad = false;
	// Android
	if(android){
		Nuui.os = 'android';
		Nuui.android = true;
		// 测试native代码是否可用
		try{
			window.MsgJs.hideWaitPanel("");
		}catch(e){
			Nuui.os = 'web';
			Nuui.android = false;
		}
	}
	if((ipad || iphone || ipod)&&isNative){
		Nuui.os = 'ios';
		Nuui.ios = true;
	}
	// iOS
	if(iphone && !ipod){
		Nuui.osVersion = iphone[2].replace(/_/g, '.');
		Nuui.iphone = true;
	}
	if(ipad){
		Nuui.osVersion = ipad[2].replace(/_/g, '.');
		Nuui.ipad = true;
	}
	if(ipod){
		Nuui.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
		Nuui.iphone = true;
	}
	// iOS 8+ changed UA
	if(Nuui.ios && Nuui.osVersion && ua.indexOf('Version/') >= 0){
		if(Nuui.osVersion.split('.')[0] === '10'){
			Nuui.osVersion = ua.toLowerCase().split('version/')[1].split(' ')[0];
		}
	}
	//本地开发 Nuui.os = web
	//Nuui.os = "web";
	if(Nuui.iphone || Nuui.android){
		Nuui.phone = true;
	}

	document.body.className = Nuui.os;
	
	
})(window);
